//
//  JPAPIBaseManager.h
//  JPWords
//
//  Created by 梁淑婷 on 2024/4/20.
//

#import <Foundation/Foundation.h>
#import "JPURLResponse.h"
#import "JPNetworkingDefines.h"

NS_ASSUME_NONNULL_BEGIN

@interface JPAPIBaseManager : NSObject<NSCopying>

// outter functions
@property (nonatomic, weak) id <JPAPIManagerCallBackDelegate> delegate;
@property (nonatomic, weak) id <JPAPIManagerParamSource> paramSource;
@property (nonatomic, weak) id <JPAPIManagerValidator> validator;
@property (nonatomic, weak) NSObject<JPAPIManager> * child; //里面会调用到NSObject的方法，所以这里不用id
@property (nonatomic, weak) id <JPAPIManagerInterceptor> interceptor;


// response
@property (nonatomic, strong) JPURLResponse * response;
@property (nonatomic, readonly) JPAPIManagerErrorType errorType;
@property (nonatomic, copy, readonly) NSString * errorMessage;

// before loading
@property (nonatomic, assign, readonly) BOOL isReachable;
@property (nonatomic, assign, readonly) BOOL isLoading;

// start
- (NSInteger)loadData;
- (NSInteger)loadDataWithParams:(NSDictionary *)params success:(void (^)(JPAPIBaseManager *manager))successCallback fail:(void (^)(JPAPIBaseManager *manager))failCallback;
- (NSInteger)loadDataWithSuccess:(void (^)(JPAPIBaseManager *manager))successCallback fail:(void (^)(JPAPIBaseManager *manager))failCallback;


+ (NSInteger)loadDataWithParams:(NSDictionary *)params success:(void (^)(JPAPIBaseManager * apiManager))successCallback fail:(void (^)(JPAPIBaseManager * apiManager))failCallback;

// cancel
- (void)cancelAllRequests;
- (void)cancelRequestWithRequestId:(NSInteger)requestID;

// finish
- (id)fetchDataWithReformer:(id <JPAPIManagerDataReformer>)reformer;
- (void)cleanData;

@end


@interface JPAPIBaseManager (InnerInterceptor)

- (BOOL)beforePerformSuccessWithResponse:(JPURLResponse *)response;
- (void)afterPerformSuccessWithResponse:(JPURLResponse *)response;

- (BOOL)beforePerformFailWithResponse:(JPURLResponse *)response;
- (void)afterPerformFailWithResponse:(JPURLResponse *)response;

- (BOOL)shouldCallAPIWithParams:(NSDictionary *)params;
- (void)afterCallingAPIWithParams:(NSDictionary *)params;

@end



@interface JPAPIBaseManager (Analyze)

- (BOOL)needLoginByResponse;

- (NSString *)messageByResponse;


@end

NS_ASSUME_NONNULL_END
